package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.db.Settings;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.server.UserService;
import com.tanhua.server.server.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserService userService;
    @Autowired
    private UsersService usersService;
//    解析传两次id，userID！=null 表示查询其他用户得id
//      userID==null，表示查自己得id，解析token里得id
    @GetMapping   //此处不要写东西，上边有路径了
    public UserInfoVo findById(@RequestHeader("Authorization") String token,Long userID){
        Long userId = userID;
        if(userID==null){  //当前userID为空，说明查询自己得信息
            //判断tokrn
            boolean b = JwtUtils.verifyToken(token);
            if(!b){
                throw new RuntimeException("未登录");
        }
            //解析token
//            Claims claims = JwtUtils.getClaims(token);
//           userId = claims.get("id", Long.class);
            userId = UserHolder.getUserId();
        }
        UserInfoVo userInfoVo = usersService.findById(userId);
        return userInfoVo;
    }
    @PutMapping
    public void update(@RequestHeader("Authorization") String token, @RequestBody UserInfo userInfo){
//        boolean b = JwtUtils.verifyToken(token);
//        if (!b){
//            throw new RuntimeException("用户未登录");
//        }
        //发请求过来没发id
//        Claims claims = JwtUtils.getClaims(token);
//        Long usrtId = claims.get("id",Long.class);
        Long userId = UserHolder.getUserId();
        userInfo.setId(userId);
        usersService.update(userInfo);

    }
    @PostMapping("/notifications/setting")
    public ResponseEntity setting(@RequestBody Map map){
        Boolean likeNotification = (Boolean) map.get("likeNotification");
        Boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        Boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");
        usersService.saveSetting(likeNotification,pinglunNotification,gonggaoNotification);
        return ResponseEntity.ok(null);
    }
    @GetMapping("/settings")
    public ResponseEntity settings(){
        SettingsVo settingsVo = usersService.findSettings();
        return ResponseEntity.ok().body(settingsVo);
    }
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){
        String content = (String)map.get("content");
        usersService.saveQuestion(content);
        return ResponseEntity.ok().body(null);
    }
    @GetMapping("/blacklist")  //query类型参数即？后的，直接写就行
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10")Integer pagesize){
      PageResult pageResult = usersService.selectBlackList(page,pagesize);
      return ResponseEntity.ok().body(pageResult);
    }
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity  delblacklist(@PathVariable("uid") Long blackId){
        usersService.delBlack(blackId);
     return ResponseEntity.ok().body(null);
    }
}
